﻿using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using System;

namespace DotnetCore.Module.Extension
{
    public static class ModuleExtension
    {

        public static void AddDcModule(this IServiceCollection services, Type startModule)
        {
            var moduleManager = DcModuleManager.Get();
            moduleManager.Initialize(startModule);
            moduleManager.Register(services);
        }

        public static void AddDcModule<THostedService>(this IServiceCollection services, Type startModule)
            where THostedService : class, IHostedService
        {
            services.AddDcModule(startModule);
            services.AddHostedService<THostedService>();
        }

        public static void AddDcModule<THostedService>(this IServiceCollection services, Type startModule,Action action)
            where THostedService : class, IHostedService
        {
            services.AddDcModule(startModule);
            services.AddHostedService<THostedService>();
            action?.Invoke();
        }

        public static void UseMcModule(this IApplicationBuilder app, Action<IDcModuleManager> action)
        {
            var moduleManager = DcModuleManager.Get();
            moduleManager.StartModule();
            action(moduleManager);
        }
    }
}
